package Tools;

import java.awt.Color;

import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class Library {
	
	/**
	 * Appends colored text to a JTextPane
	 * @param jtp
	 * @param c
	 * @param s
	 */
	public static void appendToJTP(JTextPane jtp, Color c, String s) 
	{ 
		StyleContext sc = StyleContext.getDefaultStyleContext();
	    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
	                                       StyleConstants.Foreground, c);
	    int len = jtp.getDocument().getLength(); // same value as getText().length();
	    jtp.setCaretPosition(len);  // place caret at the end (with no selection)
	    jtp.setCharacterAttributes(aset, false);
	    jtp.replaceSelection(s); // there is no selection, so inserts at caret
	}
	
}
